package com.hyk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyk.dto.CoinDto;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyk.mapper.CoinMapper;
import com.hyk.domain.Coin;
import com.hyk.service.CoinService;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CoinServiceImpl extends ServiceImpl<CoinMapper, Coin> implements CoinService{

    @Override
    public Coin findCoinByName(String coinName) {

        QueryWrapper<Coin> wrapper = new QueryWrapper<>();
        wrapper.eq("name",coinName);
        wrapper.orderByDesc("created");
        List<Coin> list = list(wrapper);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    @Override
    public Map<Long, CoinDto> getCoinByIds(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return null;
        }
        Map<Long,CoinDto> map = new HashMap<>();
        List<Coin> list = list(new LambdaQueryWrapper<Coin>().in(Coin::getId,ids));
        for(Coin coin : list){
            CoinDto coinDto = new CoinDto();
            BeanUtils.copyProperties(coin,coinDto);
            map.put(coinDto.getId(),coinDto);
        }
        return map;
    }
}
